﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using FileSystem.Models;
using FileSystem.Services.IServices;
using FileSystem.Views;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Xamarin.Forms;

namespace FileSystem.ViewModels {
    /// <summary>
    /// 登录页面ViewModel
    /// </summary>
    public class LoginPageViewModel : ViewModelBase {
        // ******** 构造函数

        /// <summary>
        /// 系统接口
        /// </summary>
        private ISystem _system;

        /// <summary>
        /// 根导航服务
        /// </summary>
        private IRootNavigationService _rootNavigationService;
        public LoginPageViewModel(ISystem system, IRootNavigationService rootNavigationService) {
            _system = system;
            _rootNavigationService = rootNavigationService;
        }

        // ******** 绑定属性
        /// <summary>
        /// 要登录的用户
        /// </summary>
        public User User {
            get => _user;
            set => Set(nameof(User), ref _user, value);
        }

        /// <summary>
        /// 要登录的用户
        /// </summary>
        private User _user;

        /// <summary>
        /// 密码
        /// </summary>
        public string Password {
            get => _password;
            set => Set(nameof(Password), ref _password, value);
        }

        /// <summary>
        /// 密码
        /// </summary>
        private string _password;

        // ******** 绑定命令

        /// <summary>
        /// 登录命令
        /// </summary>
        private RelayCommand _loginCommand;

        /// <summary>
        /// 登录命令
        /// </summary>
        public RelayCommand LoginCommand =>
            _loginCommand ?? (_loginCommand = new RelayCommand(async () => await LoginCommandFunction()));

        public async Task LoginCommandFunction() {
            if (await _system.Login(User.UserName, Password)) {
                UsersPageViewModel.CurrentUser = User;
                // await _rootNavigationService.NavigateToAsync(RootNavigationConstants
                //     .MainPage);
                await NavigationPage.PopToRootAsync();
                Password = "";
            } else {
                await MainPage.DisplayAlert("提示", "密码输入错误,请重新输入","确定");
                Password = "";
            }
        }

        // ******** 公开变量

        // ******** 私有变量

        /// <summary>
        /// NavigationPage
        /// </summary>
        public NavigationPage NavigationPage =>
            _navigationPage ?? (_navigationPage = Application.Current.MainPage as NavigationPage);

        /// <summary>
        /// NavigationPage
        /// </summary>
        private NavigationPage _navigationPage;

        /// <summary>
        /// MainPage
        /// </summary>
        public MainPage MainPage =>
            _mainPage ?? (_mainPage = NavigationPage.RootPage as MainPage);

        /// <summary>
        /// MainPage
        /// </summary>
        private MainPage _mainPage;
    }
}